<template>
  <div>
    <Breadcrumb :opt-data="genFuncOptData"></Breadcrumb>
    <PageLayout>
      <div class="table-container height-100">
        <EZTable
          v-loading="tableLoading"
          :table-id="tableId"
          :table-hdl="tableHdl"
          :rows="genFuncData"
          :custom-empty="true"
          @pageChanged="getGenFuncData"
          @sizeChanged="getGenFuncData"
          @filterChange="getGenFuncData">
          <EmptyContent slot="empty" :opt-data="genFuncOptData"></EmptyContent>
        </EZTable>
      </div>
      <el-dialog
        :title="`${!isEditGenFunc ? '新增' : '编辑'}方法名`"
        :visible.sync="showGenFuncDialog"
        :close-on-click-modal="false"
        width="700px"
        center>
        <el-form
          v-loading="genFuncFormLoading"
          :ref="genFuncFormRef"
          :model="genFuncForm"
          :rules="genFuncFormRules"
          size="small"
          label-width="130px">
          <el-form-item label="设备类别" prop="deviceType">
            <el-select v-model="genFuncForm.deviceType" clearable filterable class="width-100"
                       placeholder="请选择设备类别">
              <el-option :label="deviceType.label" :value="deviceType.value" :key="idx"
                         v-for="(deviceType, idx) in deviceTypeData"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="命令类型" prop="type">
            <el-radio-group v-model="genFuncForm.type">
              <el-radio :label="type.value" :key="idx"
                        v-for="(type, idx) in commonConfig.commandType">{{type.label}}
              </el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="方法名 (英文)" prop="name">
            <el-input v-model="genFuncForm.name" placeholder="请填写方法名"></el-input>
          </el-form-item>
          <el-form-item label="方法别名" prop="alias">
            <el-input v-model="genFuncForm.alias" placeholder="请填写方法别名"></el-input>
          </el-form-item>
          <el-form-item label="备注">
            <el-input type="textarea" v-model="genFuncForm.remarks" placeholder="请填写备注"></el-input>
          </el-form-item>
          <div class="text-align-right">
            <el-form-item>
              <el-button size="medium" @click="genFuncFormCanceled">取消</el-button>
              <el-button size="medium" type="primary" @click="genFuncFormSubmitted">提交</el-button>
            </el-form-item>
          </div>
        </el-form>
      </el-dialog>
    </PageLayout>
  </div>
</template>

<script>
  import {commonConfig} from "@/config/common";
  import {tableIdConfig, tableOptBtnConfig} from '@/config/table';
  import {commonString, msgCode} from "@/config/string";
  import {confirmHandle, deepCopyWithJson, messageHandle} from "@/utils";
  import {assembleTableOptBtn, getTableConfig, TableStateHdl} from "@/modules/routeParamStateHdl";
  import generalFuncTableCfg from "@/config/table/generalFunc";
  import EZTable from '@/components/table/EZTable';

  export default {
    name: "GeneralFunc",
    components: {
      EZTable
    },
    data() {
      return {
        commonConfig: commonConfig,
        tableLoading: true,
        tableId: tableIdConfig.generalFunc,
        tableHdl: null,
        genFuncData: [],
        showGenFuncDialog: false,
        genFuncFormLoading: false,
        genFuncForm: {},
        genFuncFormRules: {
          deviceType: [
            {required: true, message: '请选择设备类别', trigger: 'blur'}
          ],
          type: [
            {required: true, message: '请选择命令类型', trigger: 'blur'}
          ],
          name: [
            {required: true, message: '请填写方法名', trigger: 'blur'}
          ],
          alias: [
            {required: true, message: '请填写方法别名', trigger: 'blur'}
          ],
        },
        genFuncFormRef: 'genFuncFormRef',
        deviceTypeData: [],
        isEditGenFunc: false
      }
    },
    computed: {
      genFuncOptData() {
        return [
          {
            type: 'btn',
            label: '新增通用方法名',
            icon: 'icon-tianjia',
            callback: () => {
              this.clickCreateBtn();
            }
          }
        ];
      }
    },
    activated() {
      this.initTableConfig();
      this.initGenFuncForm();
      this.getDeviceTypeData();
      this.getGenFuncData();
    },
    methods: {
      initTableConfig() {
        if (this.tableHdl) return;
        const {columns = [], filters = []} = getTableConfig(this.tableId, generalFuncTableCfg);
        assembleTableOptBtn(columns, ({type = '', data = {}}) => {
          if (!type) return;
          const generalFunc = deepCopyWithJson(data);
          switch (type) {
            case tableOptBtnConfig.edit.type:
              this.showGenFuncDialog = true;
              this.isEditGenFunc = true;
              this.genFuncForm = generalFunc;
              break;
            case tableOptBtnConfig.copy.type:
              this.showGenFuncDialog = true;
              this.isEditGenFunc = false;
              this.genFuncForm = generalFunc;
              delete this.genFuncForm['_id'];
              break;
            case tableOptBtnConfig.delete.type:
              this.deleteGenFunc(generalFunc);
              break;
            default:
              break;
          }
        });
        this.tableHdl = new TableStateHdl(this.$route.name, this.tableId, columns, filters);
      },
      initGenFuncForm() {
        this.genFuncForm = {
          deviceType: '',
          type: commonConfig.commandType[0].value,
          name: '',
          alias: '',
          remarks: '',
        }
      },
      resetGenFuncForm() {
        this.initGenFuncForm();
        const genFuncForm = this.$refs[this.genFuncFormRef];
        genFuncForm && genFuncForm.resetFields();
      },
      clickCreateBtn() {
        this.isEditGenFunc && this.resetGenFuncForm();
        this.showGenFuncDialog = true;
        this.isEditGenFunc = false;
      },
      getDeviceTypeData() {
        API.deviceType.deviceTypeAll().then(result => {
          const {data = []} = result;
          this.deviceTypeData = data;
        }).catch(err => {
          console.log('err', err)
        });
      },
      getGenFuncData() {
        this.tableLoading = true;
        const selector = {};
        const tableParam = this.tableHdl.assembleTableParam();
        API.genFunc.genFuncList({...tableParam, selector}).then(result => {
          this.tableLoading = false;
          const {data = [], total = 0} = result;
          this.genFuncData = data;
          this.tableHdl.total = total;
        }).catch(err => {
          console.log('err', err)
          this.tableLoading = false;
        });
      },
      deleteGenFunc(genFunc = {}) {
        confirmHandle({label: `确定要删除通用方法名：${genFunc.name} 吗?`}).then(() => {
          API.genFunc.genFuncDelete({_id: genFunc._id}).then(result => {
            messageHandle({code: msgCode.DELETE_SUCCESS, title: '通用方法名'});
            this.tableHdl.judgeTableMatched();
            this.getGenFuncData();
            this.resetGenFuncForm();
          });
        }).catch(() => {
        });
      },
      genFuncFormCanceled() {
        this.showGenFuncDialog = false;
      },
      genFuncFormSubmitted() {
        this.$refs[this.genFuncFormRef].validate((valid) => {
          if (!valid) return;
          this.genFuncFormLoading = true;
          if (!this.isEditGenFunc) {
            API.genFunc.genFuncAdd({...this.genFuncForm}).then(result => {
              messageHandle({code: msgCode.ADD_SUCCESS, title: '通用方法名'});
              this.showGenFuncDialog = false;
              this.genFuncFormLoading = false;
              this.getGenFuncData();
              this.resetGenFuncForm();
            }).catch(err => {
              console.log('err', err)
              this.genFuncFormLoading = false;
            });
          } else {
            API.genFunc.genFuncEdit({...this.genFuncForm}).then(result => {
              messageHandle({code: msgCode.EDIT_SUCCESS, title: '通用方法名'});
              this.showGenFuncDialog = false;
              this.genFuncFormLoading = false;
              this.getGenFuncData();
              this.resetGenFuncForm();
            }).catch(err => {
              console.log('err', err)
              this.genFuncFormLoading = false;
            });
          }
        });
      },
    },
  }
</script>

<style scoped>

</style>
